package samplebasic

import sweet.mvc.api._
class FormDemo2Controller extends FormController {  
  val form = new Form("demo"){
    addField("username", "Username", StringConverter){
      VRequired() ++ VLen(3, 32, "Length must between 3 to 32 characters")
    }
    addField("password", "Password", StringConverter){
      VRequired() ++ VLen(3, 32, "Length must between 3 to 32 characters")
    }
    addField("cpassword", "Confirm Password", StringConverter){
      VRequired() ++ VLen(3, 32, "Length must between 3 to 32 characters") ++
      VFunc{ (fieldValue, fieldName, formData) => 
        if(fieldValue != formData("password")){
          formData.addError(fieldName, "Confirmation password not match.")
          false
        }else{ true }
      }
    }
       
    addField("firstName", "First Name", StringConverter){
      VOptional() ++ VLen(3, 128, "Length must between 3 to 128 characters")
    }
    addField("lastName", "Last Name", StringConverter){
      VOptional() ++ VLen(3, 128, "Length must between 3 to 128 characters")
    }
    addField("email", "Email", StringConverter){
      VOptional() ++ VLen(5, 128, "Length must between 5 to 128 characters") ++
      VValue("Email must be in <name>@<domain> format."){ value =>
        value.matches("""^[A-Za-z0-9\\.\-_]+@[A-Za-z0-9\\.\-_]+$""") 
      }
    }
  }
  
  handleForm("/index", form){ 
    new FormHandler(){
      def viewForm(formData: FormData, req: Req) =  HandlerNameView()
      def processForm(formData: FormData, req: Req) = HandlerNameView("submitSuccessful"->true)
    }
  }
}

